package com.xmy.cultivate.controller;

import com.xmy.cultivate.service.*;
import com.xmy.cultivate.service.IRecordCourseService;
import com.xmy.cultivate.service.IStudentGradeService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;


/**
 * <p>
 * 记止课 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2022-08-10
 */
@RestController
@RequestMapping("/api/recordCourse")
@Scope("prototype")
public class RecordCourseController{

    @Autowired
    IRecordCourseService iRecordCourseService;

    @Autowired
    IStudentGradeService iStudentGradeService;

    @Autowired
    IWecharPushService iWecharPushService;

    @Autowired
    IStudentResidueCourseService iStudentResidueCourseService;

    @Autowired
    IStudentGradeHistoryService iStudentGradeHistoryService;

    @Autowired
    IMqSchedulingLogService iMqSchedulingLogService;

    @GetMapping("/autoRecordAction")
    public Result autoRecordAction(@RequestParam("nowLocalDate") String nowLocalDateStr,
                                   @RequestParam("localDateTime") String localDateTimeStr){
        try {
            LocalDateTime localDateTime = LocalDateTime.parse(localDateTimeStr);
            LocalDate localDate = LocalDate.parse(nowLocalDateStr);
            if (localDate.isAfter(LocalDate.now())){
                return new Result(Code.OK,"","日期不能超过当前日期");
            }
            if (localDateTime.isAfter(LocalDateTime.now())){
                return new Result(Code.OK,"","日期时间不能超过当前日期时间");
            }


            iRecordCourseService.autoRecordAction(localDate,localDateTime);
            return new Result(Code.OK,"","成功");
        }catch (Exception e){
            return new Result(Code.OK,e.getMessage(),"失败");
        }
    }

    @GetMapping("/cancClassRemind")
    public Result cancClassRemind(){
        iWecharPushService.cancClassRemind();
        return new Result(Code.OK,"","推送中");
    }


    @GetMapping("/classRemind")
    public Result classRemind(){
        iMqSchedulingLogService.addData(2024,2,"2024-06-14","","","1");
        return new Result(Code.OK,"","推送中");
    }

    @GetMapping("/updateStudentIsPresort")
    public Result updateStudentIsPresort(){
        iStudentGradeService.updateStudentIsPresort();
        return new Result(Code.OK,"","");
    }

    @GetMapping("/studentClassGradePushMq")
    public Result studentClassGradePushMq(@RequestParam("yearPart") Integer yearPart,
                                          @RequestParam("quarterNum") Integer quarterNum,
                                          @RequestParam("schoolId") Long schoolId,
                                          @RequestParam("subjectsId") Long subjectsId
                                          ){
        iStudentGradeService.studentClassGradePushMq(yearPart,quarterNum,schoolId,subjectsId);
        return new Result(Code.OK,"","");
    }

    /**
     * 课程顺延前剩余课次记录添加
     * @param yearPart
     * @param quarterNum
     * @param lessonType
     * @return
     */
    @GetMapping("/addResidueCourse")
    public Result addResidueCourse(@RequestParam("yearPart") Integer yearPart,
                                          @RequestParam("quarterNum") Integer quarterNum,
                                          @RequestParam(value = "lessonType",required = false,defaultValue = "1") Integer lessonType
    ){
        iStudentResidueCourseService.addBatch(yearPart,quarterNum,lessonType);
        return new Result(Code.OK,"","");
    }

    @PostMapping("/addStudentGradeHistory")
    public Result addStudentGradeHistory(@RequestParam("yearPart") Integer yearPart,
                                         @RequestParam("quarterNum") Integer quarterNum){
        iStudentGradeHistoryService.addStudentGradeHistory(yearPart,quarterNum,null,null);
        return new Result(Code.OK,"","");
    }


}
